/*
 * Copyrignt (c) xuzeshui.com. All Rights Reserved.
 * Author: Zeshui Xu<i@xuzeshui.com>
 * Created Time: 2015-08-22 14:38:49
 * Last Modified: 2017-05-05 16:03:23
 * File Name: auth/appkey.go
 * Description:
 */
package auth

import (
//"sync"
)

var (
	authAppKey *authAppKeyService
)

//每个appkey的基本信息
type AppKeyItem struct {
	Desc      string //appkey的描述
	SecretKey string //私钥
}

func newAppKeyItem(desc, secrect string) *AppKeyItem {
	return &AppKeyItem{
		Desc:      desc,
		SecretKey: secrect,
	}
}

type authAppKeyService struct {
	appKeyInfo map[string]*AppKeyItem
	//appkeyInfoLock *sync.RWMutex
}

/*
	构造authAppKeyService实例
*/
func newAuthAppKeyService() *authAppKeyService {
	service := &authAppKeyService{
		appKeyInfo: make(map[string]*AppKeyItem),
		//appkeyInfoLock: new(sync.RWMutex),
	}
	service.initAppKeyInfo()
	return service
}

func GetAuthAppKeyService() *authAppKeyService {
	if authAppKey == nil {
		authAppKey = newAuthAppKeyService()
	}
	return authAppKey
}

//初始化加载配置,当前为硬编码
func (p *authAppKeyService) initAppKeyInfo() {
	p.appKeyInfo["19854c206c2f4a82b33ce98872ab31aa"] = newAppKeyItem("qingkeleague", "eb90e35a31b63415f717482d209f8aa2")
	p.appKeyInfo["af7d08b2f8407d907bfbf1a1c3d5dbfd"] = newAppKeyItem("h5", "dfa8c712433b83b8d827d9386fa2fa46")
}

//验证appkey是否有效
func (p *authAppKeyService) ValidateAppKey(appkey string) bool {
	_, ok := p.GetAppKeyItem(appkey)
	return ok
}

//根据appkey获取appkey的基本信息
func (p *authAppKeyService) GetAppKeyItem(appkey string) (*AppKeyItem, bool) {
	item, ok := p.appKeyInfo[appkey]
	return item, ok
}

//获取appkey对应的私钥
func (p *authAppKeyService) GetSecretKey(appkey string) (string, bool) {
	item, ok := p.GetAppKeyItem(appkey)
	if !ok {
		return "", false
	}
	return item.SecretKey, true
}
